package pub.cleangao.mybatis.springboot;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Order(Integer.MAX_VALUE)
@Component
public class DSAnnotationAspect {

    @Pointcut(value = "@annotation(pub.cleangao.mybatis.springboot.DS)")
    public void methodPointcut() {
    }

    @Around("@annotation(ds)")
    public Object around(ProceedingJoinPoint point, DS ds) throws Throwable {
        String currentDataSourceKey = ds.value();
        DataSourceContext.setDataSourceKey(currentDataSourceKey);
        try {
            return point.proceed();
        } finally {
            DataSourceContext.clearDataSourceKey();
        }
    }
}
